package cn.tedu.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定的构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Person p = new Person("李四",33);
        System.out.println(p);

        //1加载类对象
        Class cls = Class.forName("reflect.Person");
        //2通过类对象获取指定的有参构造器：Person(String,int)
//        Constructor c = cls.getConstructor();//不传参获取的就是无参构造器
        Constructor c = cls.getConstructor(String.class,int.class);
        Object obj = c.newInstance("王五",66);//new Person("王五",66);
        System.out.println(obj);
    }
}
